﻿using Vibstudio.NetConfigurator.UI.Util;

namespace Vibstudio.NetConfigurator.UI
{
    #region Namespaces

    using System;
    using System.Configuration;
    using System.Diagnostics;
    using System.IO;

    using Vibstudio.NetConfigurator.Globalization;
    using Vibstudio.NetConfigurator.UI.Common;

    #endregion

    public partial class Shell
    {
        private const string APP_CONFIG_NAME = "app.config";

        #region Ctor(s)

        public Shell()
        {
            InitializeComponent();
            SetAppConfig();
            SetLang();
            AdjustUI();
            DataContext = ViewModelLocator.ShellViewModel;
            ViewModelLocator.SetChild(ViewRepository.Get.Home());
        }

        #endregion

        #region Methods

        private void AdjustUI()
        {
            OSPlatformVersion osPlatformVersion = OS.GetPlatformVersion();
            if (osPlatformVersion == OSPlatformVersion.WindowsXP)
            {
                Height = 265;
            }
        }

        private string GetAppLang()
        {
            string lang = !String.IsNullOrEmpty(ConfigurationManager.AppSettings.Get("lang"))
                              ? ConfigurationManager.AppSettings.Get("lang")
                              : System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

            Trace.WriteLine(string.Format("lang = {0}", string.IsNullOrEmpty(lang) ? "null" : lang), "Shell.GetAppLang");

            return lang;
        }

        private void SetAppConfig()
        {
            string appConfigPath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, APP_CONFIG_NAME);
            if (File.Exists(appConfigPath))
            {
                AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", appConfigPath);
            }
        }

        private void SetLang()
        {
            Labeler.Setup(GetAppLang());
        }

        #endregion
    }
}
